package com.wgts.wgts.mvp.view.activity.mycenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.dialog.ToastDate;
import com.wgts.wgts.control.dialog.ToastDialogFragment;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerShippingAddressActivityComponent;
import com.wgts.wgts.dagger.module.ShippingAddressActivityModule;
import com.wgts.wgts.mvp.presenter.IShippingAddressActivityPresenter;
import com.wgts.wgts.mvp.view.IShippingAddressActivityView;
import com.wgts.wgts.netmodle.resp.ShippingAddressEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *
 *
 *   data:2018/9/2815:12
 *
 */public class ShippingAddressActivity extends BaseActivity implements IShippingAddressActivityView {
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.shipping_address_recycle)
    RecyclerView shippingAddressRecycle;
    @BindView(R.id.add_new_address)
    LinearLayout addNewAddress;
    @BindView(R.id.none_data_ly)
    LinearLayout noneDataLy;
    @BindView(R.id.net_error_ly)
    LinearLayout netErrorLy;

    private AddressRecycleAdapter adapter;
    public static final int AddAddressResult = 0;
    private int AddressResult = 1;
    public static final String AFFIRM_ORDER_ADDRESS_RESULT = "affirm_order_address_result";
    private String affirmAddressResult;
    private List<ShippingAddressEntity> addressList;
    //判断是否是点击列表返回还是添加地址过后直接返回
    private boolean isClick = false;

    @Inject
    IShippingAddressActivityPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_center_shipping_adress_activity);
        ButterKnife.bind(this);
        initView();
        Intent intent = getIntent();
        affirmAddressResult = intent.getStringExtra(AFFIRM_ORDER_ADDRESS_RESULT);
        includeTitle.setText("收货地址");
    }

    @Override
    public void init() {
        DaggerShippingAddressActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .shippingAddressActivityModule(new ShippingAddressActivityModule(this))
                .build()
                .inject(this);
        addressList = new ArrayList<>();
    }

    private void initView() {
        shippingAddressRecycle.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AddressRecycleAdapter(this, addressList, presenter, getSupportFragmentManager());
        shippingAddressRecycle.setAdapter(adapter);
        itemClick();
        showDialog();
        presenter.getShippingAddress();
    }

    @OnClick({R.id.back_btn_ly, R.id.add_new_address, R.id.net_error_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                finish();
                break;
            case R.id.add_new_address:
                Intent intent = new Intent(ShippingAddressActivity.this, AddAddressActivity.class);
                startActivityForResult(intent, AddAddressResult);
                break;
            case R.id.net_error_ly:
                presenter.getShippingAddress();
                break;
        }
    }

    @Override
    public void onError(int status, String msg) {
        dialogDismiss();
        netErrorLy.setVisibility(View.VISIBLE);
        noneDataLy.setVisibility(View.GONE);
        MoHuToast.showToast(msg);
    }

    @Override
    public void getShippingAddress(List<ShippingAddressEntity> entityList) {
        if (entityList.size() > 0){
            netErrorLy.setVisibility(View.GONE);
            noneDataLy.setVisibility(View.GONE);
        }else {
            netErrorLy.setVisibility(View.GONE);
            noneDataLy.setVisibility(View.VISIBLE);
        }
        dialogDismiss();
        addressList.clear();
        addressList.addAll(entityList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void dateNotify() {
        dialogDismiss();
        presenter.getShippingAddress();
    }


    @Override
    public void affirmAddressResult(ShippingAddressEntity entity) {
        isClick = true;
        Intent intent = new Intent();
        intent.putExtra("address", entity);
        setResult(AddressResult, intent);
        finish();
    }


    private void itemClick() {
        adapter.setOnItemClickListen(new AddressRecycleAdapter.onItemClickListen() {
            @Override
            public void onItemClick(View view, final int position) {
                if (affirmAddressResult != null && !affirmAddressResult.equals("")) {
                    //确认订单的地址选择，直接返回
                    presenter.affirmAddressResult(position);
                } else {
                    if (position == 0){
                        MoHuToast.showToast("该地址已是默认地址");
                    }else {
                        ToastDate toastDate = new ToastDate();
                        toastDate.setCancelQuestion("是否使用此地址作为收货地址？");
                        toastDate.setCancelText("取消");
                        toastDate.setPointText("确定");
                        final ToastDialogFragment fragment = ToastDialogFragment.setToastMessage(toastDate);
                        fragment.show(getSupportFragmentManager(), "ChooseAddressDialog");
                        fragment.setOnClickListener(new ToastDialogFragment.OnClickListener() {
                            @Override
                            public void onClickCancle() {
                                showDialog();
                                presenter.normalAddressResult(addressList.get(position).addressId);
                            }

                            @Override
                            public void onClickEnsuer() {
                                fragment.dismiss();
                            }
                        });
                    }
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AddAddressResult && data != null) {
            presenter.getShippingAddress();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            /*if (affirmAddressResult != null && !affirmAddressResult.equals("")){
                if (!isClick){
                    presenter.getSelectAddress();
                }
            }else {
                finish();
            }*/
        }

        return super.onKeyDown(keyCode, event);
    }

}
